var util = require('../../libs/util.js');
var app = getApp()
Page({
  data: {
    userInfo: {},
    truename: "",
    plate: "",
    driver_id: "",
    vehicle_state: "",
    vehicle_id: "",
    vehicle_owner: "",
    totalService: {
      orderNumber: "0",
      mile: "0",
      duration: "0"
    },
    monthService: {
      orderNumber: "0",
      mile: "0",
      duration: "0"
    },
    work_status: true,
    driverLevel: '零星',
    vehicleLevel: '零星'
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": "车辆档案" });
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    //更新数据
    that.setData({
      vehicle_id: e.id,
      tab: e.tab || 0,
      role_default:app.globalData.role_default
    });
    util.getData("/wechat/vehicle/getVehicleBusinessData", { vehicle_id: that.data.vehicle_id }, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        var vehicle_data = res.data.data[0][0], //基本信息
          totalData = res.data.data[1][0], //累计接单、里程和时长
          monthData = res.data.data[2][0],//本月接单、里程和时长
          lastMonthData = res.data.data[3][0],//上月接单、里程和时长
          monthAttendance = res.data.data[4].length,//本月出勤
          lastMonthAttendance = res.data.data[5].length//上月出勤

        wx.setNavigationBarTitle({ "title": "车辆档案--" + vehicle_data.plate });

        that.setData({
          plate: vehicle_data.plate,
          iot_box: vehicle_data.iot_box,
          totalService: {
            orderNumber: totalData.count,
            mile: Number(totalData.mile).toFixed(0),
            duration: Number(totalData.duration).toFixed(0)
          },
          monthIncome: { 
            orderNumber: monthData.count,
            income: monthData.total_price
          },
          lastMonthIncome: {
            orderNumber: lastMonthData.count,
            income: lastMonthData.total_price
          },
          monthPay: {
            normal: 0,
            pay: vehicle_data.is_employee == '0' ? monthData.franchiseDriverPrice : monthData.driverPrice
          },
          lastMonthPay: {
            normal: 0,
            pay: vehicle_data.is_employee == '0' ? lastMonthData.franchiseDriverPrice : lastMonthData.driverPrice
          },
          attendance:{
            month:monthAttendance,
            lastMonth:lastMonthAttendance
          }
        });
        that.setData({
          profit: {
            month: Number(that.data.monthIncome.income) - Number(that.data.monthPay.pay) - Number(that.data.monthPay.normal),
            lastMonth: Number(that.data.lastMonthIncome.income) - Number(that.data.lastMonthPay.pay) - Number(that.data.lastMonthPay.normal)
          }
        })
      }
      wx.hideLoading();
    });
  },
  changeWorkStatus: function () {
    this.setData({
      work_status: !this.data.work_status
    })
  },
  changeVehicle: function () {
    wx.navigateTo({
      url: '/pages/driver/changeVehicle?driver=' + this.data.driver_id + '&vehicle=' + this.data.vehicle_id
    })
  },
  openTrainingMiniapp: function () {
    wx.navigateToMiniProgram({
      appId: 'wx698acf81fa1cdd2d',
      path: '/pages/index/index',
      extraData: {
        mobile: app.globalData.mobile,
        name: app.globalData.truename
      },
      success(res) {

      }
    })
  },
  showVehicleLocation: function () {
    if (this.data.gps) {
      var gps = String(this.data.gps).split(",");
      wx.openLocation({
        latitude: Number(gps[0]),
        longitude: Number(gps[1]),
        scale: 28
      })
    } else {
      wx.showToast({
        title: "暂无车辆位置",
        image: '/images/wrong.svg',
        duration: 3000
      });
    }
  },
  showVehicleTrace: function () {
    util.vehicleTraceWithIotbox(this.data.iot_box,
      util.dateObjectformat(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
      util.dateObjectformat(new Date(), 'yyyy-MM-dd') + ' 23:59:59');
  },
  numberToChinese: function (number) {
    var chnNumChar = {
      1: "一月",
      2: "二月",
      3: "三月",
      4: "四月",
      5: "五月",
      6: "六月",
      7: "七月",
      8: "八月",
      9: "九月",
      10: "十月",
      11: "十一月",
      12: "十二月"
    };
    return chnNumChar[Number(number)];
  }
})
